using MediatR;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Mvc;
using Process.Api.Application.Command.BOM;
using Process.DAL;
using Process.ErrorCod;
using Process.Model;

namespace Process.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class BomController : ControllerBase
    {
        private readonly IMediator _mediator;

        public BomController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 创建BOM
        /// </summary>
        /// <param name="command">BOM创建命令</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateBom([FromBody] CreateBomCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code =ErrorStatus.Error,
                    Message = $"创建BOM失败: {ex.Message}",
                    Data = 0
                };
            }
        }
        /// <summary>
        /// BOM列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<ApiPageing<BomTable>> GetBom([FromQuery] GetBomListCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 获取BOM详情
        /// </summary>
        /// <param name="id">BOM ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<BomDetailDto>> GetBomDetail([FromQuery] int id)
        {
            try
            {
                var command = new GetBomDetailCommand { Id = id };
                var result = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                return new ApiResult<BomDetailDto>
                {
                    Code = ErrorStatus.Error,
                    Message = $"获取BOM详情失败: {ex.Message}",
                    Data = null
                };
            }
        }

        /// <summary>
        /// 删除BOM（软删除）
        /// </summary>
        /// <param name="command">删除命令</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteBom([FromBody] DeleteBomCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ErrorStatus.Error,
                    Message = $"删除BOM失败: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 更新BOM
        /// </summary>
        /// <param name="command">更新命令</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateBom([FromBody] UpdateBomCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ErrorStatus.Error,
                    Message = $"更新BOM失败: {ex.Message}",
                    Data = 0
                };
            }
        }
    }
}


